#!/data/data/com.termux/files/usr/bin/bash

# GitHub仓库管理系统 - 多语言版
CONFIG_DIR="$HOME/.config/git_manager"
CONFIG_FILE="$CONFIG_DIR/config"
DEFAULT_BASE_DIR="$HOME/git_repositories"

# 语言设置
CURRENT_LANG="zh_CN"
LANG_FILE="$CONFIG_DIR/language"

# 创建配置目录
mkdir -p "$CONFIG_DIR"

# 加载配置
if [ -f "$CONFIG_FILE" ]; then
    source "$CONFIG_FILE"
else
    # 默认配置
    BASE_DIR="$DEFAULT_BASE_DIR"
    # 保存默认配置
    echo "BASE_DIR=\"$BASE_DIR\"" > "$CONFIG_FILE"
fi

# 加载保存的语言设置
if [ -f "$LANG_FILE" ]; then
    CURRENT_LANG=$(cat "$LANG_FILE")
fi

# 语言文本定义
declare -A TEXT_zh_CN TEXT_en_US

# 中文文本
TEXT_zh_CN=(
    ["title"]="GitHub仓库管理系统"
    ["enter_dir_error"]="错误：无法进入目录"
    ["select_operation"]="请选择操作:"
    ["view_all_status"]="查看所有仓库状态"
    ["pull_all_updates"]="拉取所有仓库更新" 
    ["manage_single_repo"]="管理单个仓库"
    ["batch_stage_commit"]="批量设置暂存和提交"
    ["switch_language"]="切换语言"
    ["change_base_dir"]="更改仓库目录"
    ["exit"]="退出"
    ["input_choice"]="输入选择"
    ["invalid_choice"]="无效选择，请重试"
    ["repo_status"]="📁 仓库状态:"
    ["synced"]="已同步"
    ["unsynced"]="有未提交更改"
    ["normal_dir"]="普通目录"
    ["pulling_updates"]="🔄 拉取所有仓库更新..."
    ["processing"]="处理:"
    ["all_updated"]="✅ 所有仓库更新完成"
    ["available_repos"]="可用Git仓库:"
    ["no_git_repos"]="❌ 没有找到Git仓库"
    ["select_repo_num"]="选择仓库编号:"
    ["managing_repo"]="管理仓库:"
    ["current_branch"]="当前分支:"
    ["unknown"]="未知"
    ["view_status"]="查看状态"
    ["pull_updates"]="拉取更新"
    ["view_logs"]="查看日志"
    ["stage_commit"]="设置暂存和提交"
    ["view_untracked"]="查看未跟踪文件"
    ["return_menu"]="返回主菜单"
    ["select_action"]="选择操作:"
    ["stage_commit_title"]="📝 设置暂存和提交"
    ["changed_files"]="变更文件:"
    ["stage_all"]="暂存所有变更"
    ["stage_specific"]="暂存指定文件"
    ["commit_changes"]="提交变更"
    ["stage_commit_all"]="暂存并提交所有"
    ["enter_files"]="输入要暂存的文件路径（多个文件用空格分隔）:"
    ["files"]="文件:"
    ["enter_commit_msg"]="输入提交说明:"
    ["message"]="说明:"
    ["msg_empty"]="提交说明不能为空"
    ["all_staged"]="✅ 所有变更已暂存"
    ["files_staged"]="✅ 文件已暂存"
    ["changes_committed"]="✅ 变更已提交"
    ["all_staged_committed"]="✅ 所有变更已暂存并提交"
    ["untracked_files"]="📄 未跟踪文件:"
    ["batch_stage_title"]="🔄 批量设置暂存和提交"
    ["changed_repos"]="有未提交变更"
    ["all_synced"]="✅ 所有仓库都已同步，无需操作"
    ["batch_operations"]="选择操作:"
    ["stage_commit_all_repos"]="为所有仓库暂存并提交"
    ["stage_commit_selected"]="选择特定仓库操作"
    ["enter_unified_msg"]="输入统一的提交说明:"
    ["select_repos"]="选择要操作的仓库（输入编号，多个用空格分隔）:"
    ["repo_numbers"]="仓库编号:"
    ["batch_complete"]="🎉 批量提交完成"
    ["selected_complete"]="🎉 选择仓库提交完成"
    ["goodbye"]="👋 再见！"
    ["press_enter"]="按回车键继续..."
    ["language_changed"]="语言已切换为中文"
    ["switch_to_english"]="切换到英文"
    ["switch_to_chinese"]="切换到中文"
    ["current_dir"]="当前仓库目录"
    ["change_dir_prompt"]="请输入新的仓库目录路径"
    ["dir_changed"]="✅ 目录已更改"
    ["dir_not_exist"]="❌ 目录不存在，请重新输入"
    ["config_saved"]="✅ 配置已保存"
)

# 英文文本
TEXT_en_US=(
    ["title"]="GitHub Repository Management System"
    ["enter_dir_error"]="Error: Cannot enter directory"
    ["select_operation"]="Please select operation:"
    ["view_all_status"]="View all repository status"
    ["pull_all_updates"]="Pull all repository updates"
    ["manage_single_repo"]="Manage single repository"
    ["batch_stage_commit"]="Batch stage and commit"
    ["switch_language"]="Switch language"
    ["change_base_dir"]="Change repository directory"
    ["exit"]="Exit"
    ["input_choice"]="Enter choice"
    ["invalid_choice"]="Invalid choice, please try again"
    ["repo_status"]="📁 Repository Status:"
    ["synced"]="Synced"
    ["unsynced"]="Has uncommitted changes"
    ["normal_dir"]="Normal directory"
    ["pulling_updates"]="🔄 Pulling all repository updates..."
    ["processing"]="Processing:"
    ["all_updated"]="✅ All repositories updated"
    ["available_repos"]="Available Git repositories:"
    ["no_git_repos"]="❌ No Git repositories found"
    ["select_repo_num"]="Select repository number:"
    ["managing_repo"]="Managing repository:"
    ["current_branch"]="Current branch:"
    ["unknown"]="Unknown"
    ["view_status"]="View status"
    ["pull_updates"]="Pull updates"
    ["view_logs"]="View logs"
    ["stage_commit"]="Stage and commit"
    ["view_untracked"]="View untracked files"
    ["return_menu"]="Return to main menu"
    ["select_action"]="Select action:"
    ["stage_commit_title"]="📝 Stage and Commit"
    ["changed_files"]="Changed files:"
    ["stage_all"]="Stage all changes"
    ["stage_specific"]="Stage specific files"
    ["commit_changes"]="Commit changes"
    ["stage_commit_all"]="Stage and commit all"
    ["enter_files"]="Enter file paths to stage (multiple files separated by spaces):"
    ["files"]="Files:"
    ["enter_commit_msg"]="Enter commit message:"
    ["message"]="Message:"
    ["msg_empty"]="Commit message cannot be empty"
    ["all_staged"]="✅ All changes staged"
    ["files_staged"]="✅ Files staged"
    ["changes_committed"]="✅ Changes committed"
    ["all_staged_committed"]="✅ All changes staged and committed"
    ["untracked_files"]="📄 Untracked files:"
    ["batch_stage_title"]="🔄 Batch Stage and Commit"
    ["changed_repos"]="Has uncommitted changes"
    ["all_synced"]="✅ All repositories synced, no action needed"
    ["batch_operations"]="Select operation:"
    ["stage_commit_all_repos"]="Stage and commit for all repositories"
    ["stage_commit_selected"]="Select specific repositories to operate"
    ["enter_unified_msg"]="Enter unified commit message:"
    ["select_repos"]="Select repositories to operate (enter numbers, multiple separated by spaces):"
    ["repo_numbers"]="Repository numbers:"
    ["batch_complete"]="🎉 Batch commit completed"
    ["selected_complete"]="🎉 Selected repositories commit completed"
    ["goodbye"]="👋 Goodbye!"
    ["press_enter"]="Press Enter to continue..."
    ["language_changed"]="Language switched to English"
    ["switch_to_english"]="Switch to English"
    ["switch_to_chinese"]="Switch to Chinese"
    ["current_dir"]="Current repository directory"
    ["change_dir_prompt"]="Please enter new repository directory path"
    ["dir_changed"]="✅ Directory changed"
    ["dir_not_exist"]="❌ Directory does not exist, please re-enter"
    ["config_saved"]="✅ Configuration saved"
)

# 获取文本函数
get_text() {
    local key="$1"
    local lang_var="TEXT_${CURRENT_LANG}[$key]"
    echo "${!lang_var}"
}

# 保存语言设置
save_language() {
    echo "$CURRENT_LANG" > "$LANG_FILE"
}

# 切换语言函数
switch_language() {
    if [ "$CURRENT_LANG" = "zh_CN" ]; then
        CURRENT_LANG="en_US"
        echo "$(get_text "language_changed")"
    else
        CURRENT_LANG="zh_CN"
        echo "$(get_text "language_changed")"
    fi
    save_language
}

# 更改基础目录函数
change_base_directory() {
    while true; do
        clear
        echo "========================================"
        echo "   $(get_text "change_base_dir")"
        echo "========================================"
        echo "$(get_text "current_dir"): $BASE_DIR"
        echo
        echo "$(get_text "change_dir_prompt"):"
        read -p "> " new_dir
        
        if [ -z "$new_dir" ]; then
            continue
        fi
        
        # 展开路径中的 ~
        new_dir=$(echo "$new_dir" | sed "s|^~|$HOME|")
        
        if [ -d "$new_dir" ]; then
            BASE_DIR="$new_dir"
            # 保存到配置文件
            echo "BASE_DIR=\"$BASE_DIR\"" > "$CONFIG_FILE"
            echo "$(get_text "dir_changed")"
            echo "$(get_text "config_saved")"
            read -p "$(get_text "press_enter")"
            break
        else
            echo "$(get_text "dir_not_exist")"
            read -p "$(get_text "press_enter")"
        fi
    done
}

# 检查并进入工作目录
check_and_enter_directory() {
    if [ ! -d "$BASE_DIR" ]; then
        echo "❌ $(get_text "enter_dir_error"): $BASE_DIR"
        echo "$(get_text "press_enter")"
        read
        return 1
    fi
    
    cd "$BASE_DIR" || {
        echo "❌ $(get_text "enter_dir_error"): $BASE_DIR"
        echo "$(get_text "press_enter")"
        read
        return 1
    }
    return 0
}

# 主循环
while true; do
    clear
    echo "========================================"
    echo "   $(get_text "title")"
    echo "========================================"
    echo "$(get_text "current_dir"): $BASE_DIR"
    echo

    # 检查目录
    if ! check_and_enter_directory; then
        # 如果目录不存在，显示更改目录选项
        echo
        echo "$(get_text "select_operation"):"
        echo "1. $(get_text "change_base_dir")"
        echo "2. $(get_text "exit")"
        echo
        read -p "$(get_text "input_choice") [1-2]: " choice
        
        case $choice in
            1)
                change_base_directory
                ;;
            2)
                echo "$(get_text "goodbye")"
                exit 0
                ;;
            *)
                echo "$(get_text "invalid_choice")"
                read -p "$(get_text "press_enter")"
                ;;
        esac
        continue
    fi

    echo
    echo "$(get_text "select_operation")"
    echo "1. $(get_text "view_all_status")"
    echo "2. $(get_text "pull_all_updates")"
    echo "3. $(get_text "manage_single_repo")"
    echo "4. $(get_text "batch_stage_commit")"
    echo "5. $(get_text "change_base_dir")"
    if [ "$CURRENT_LANG" = "zh_CN" ]; then
        echo "6. $(get_text "switch_to_english")"
    else
        echo "6. $(get_text "switch_to_chinese")"
    fi
    echo "7. $(get_text "exit")"
    echo
    
    read -p "$(get_text "input_choice") [1-7]: " choice

    case $choice in
        1)
            echo
            echo "$(get_text "repo_status")"
            echo "----------------------------------------"
            for dir in */; do
                if [ -d "$dir/.git" ]; then
                    echo -n "✅ ${dir%/}: "
                    cd "$dir"
                    if git status | grep -q "nothing to commit"; then
                        echo "$(get_text "synced")"
                    else
                        echo "$(get_text "unsynced")"
                    fi
                    cd ..
                else
                    echo "📂 ${dir%/}: $(get_text "normal_dir")"
                fi
            done
            read -p "$(get_text "press_enter")"
            ;;
        2)
            echo
            echo "$(get_text "pulling_updates")"
            for dir in */; do
                if [ -d "$dir/.git" ]; then
                    echo "$(get_text "processing") ${dir%/}"
                    cd "$dir" && git pull && cd ..
                    echo "----------------------------------------"
                fi
            done
            echo "$(get_text "all_updated")"
            read -p "$(get_text "press_enter")"
            ;;
        3)
            echo
            echo "$(get_text "available_repos")"
            i=1
            repo_list=()
            for dir in */; do
                if [ -d "$dir/.git" ]; then
                    echo "  $i. ${dir%/}"
                    repo_list[$i]="${dir%/}"
                    i=$((i+1))
                fi
            done

            if [ $i -eq 1 ]; then
                echo "$(get_text "no_git_repos")"
                read -p "$(get_text "press_enter")"
                continue
            fi

            echo
            read -p "$(get_text "select_repo_num"): " repo_num
            selected_repo="${repo_list[$repo_num]}"
            
            if [ -n "$selected_repo" ]; then
                cd "$selected_repo"
                
                while true; do
                    clear
                    echo "========================================"
                    echo "   $(get_text "managing_repo"): $selected_repo"
                    echo "========================================"
                    echo "$(get_text "current_branch"): $(git branch --show-current 2>/dev/null || echo "$(get_text "unknown")")"
                    echo
                    echo "1. $(get_text "view_status")"
                    echo "2. $(get_text "pull_updates")"
                    echo "3. $(get_text "view_logs")"
                    echo "4. $(get_text "stage_commit")"
                    echo "5. $(get_text "view_untracked")"
                    echo "6. $(get_text "return_menu")"
                    read -p "$(get_text "select_action"): " sub_choice
                    
                    case $sub_choice in
                        1) 
                            echo
                            git status
                            ;;
                        2) 
                            echo
                            git pull
                            ;;
                        3) 
                            echo
                            git log --oneline -5
                            ;;
                        4)
                            echo
                            echo "$(get_text "stage_commit_title")"
                            echo "----------------------------------------"
                            
                            echo "$(get_text "changed_files")"
                            git status --short
                            
                            echo
                            echo "$(get_text "select_action"):"
                            echo "1. $(get_text "stage_all")"
                            echo "2. $(get_text "stage_specific")"
                            echo "3. $(get_text "commit_changes")"
                            echo "4. $(get_text "stage_commit_all")"
                            read -p "$(get_text "input_choice") [1-4]: " stage_choice
                            
                            case $stage_choice in
                                1)
                                    git add .
                                    echo "$(get_text "all_staged")"
                                    ;;
                                2)
                                    echo "$(get_text "enter_files")"
                                    read -p "$(get_text "files"): " files
                                    if [ -n "$files" ]; then
                                        git add $files
                                        echo "$(get_text "files_staged")"
                                    fi
                                    ;;
                                3)
                                    echo "$(get_text "enter_commit_msg")"
                                    read -p "$(get_text "message"): " commit_msg
                                    if [ -n "$commit_msg" ]; then
                                        git commit -m "$commit_msg"
                                        echo "$(get_text "changes_committed")"
                                    else
                                        echo "$(get_text "msg_empty")"
                                    fi
                                    ;;
                                4)
                                    echo "$(get_text "enter_commit_msg")"
                                    read -p "$(get_text "message"): " commit_msg
                                    if [ -n "$commit_msg" ]; then
                                        git add .
                                        git commit -m "$commit_msg"
                                        echo "$(get_text "all_staged_committed")"
                                    else
                                        echo "$(get_text "msg_empty")"
                                    fi
                                    ;;
                                *)
                                    echo "$(get_text "invalid_choice")"
                                    ;;
                            esac
                            ;;
                        5)
                            echo
                            echo "$(get_text "untracked_files")"
                            git status --porcelain | grep "^??" | cut -c4-
                            ;;
                        6) 
                            cd "$BASE_DIR"
                            break
                            ;;
                        *) 
                            echo "$(get_text "invalid_choice")"
                            ;;
                    esac
                    
                    if [ "$sub_choice" != "6" ]; then
                        echo
                        read -p "$(get_text "press_enter")"
                    fi
                done
            else
                echo "$(get_text "invalid_choice")"
                read -p "$(get_text "press_enter")"
            fi
            ;;
        4)
            echo
            echo "$(get_text "batch_stage_title")"
            echo "----------------------------------------"
            
            changed_repos=()
            i=1
            for dir in */; do
                if [ -d "$dir/.git" ]; then
                    cd "$dir"
                    if ! git status | grep -q "nothing to commit"; then
                        echo "  $i. ${dir%/} - $(get_text "changed_repos")"
                        changed_repos[$i]="${dir%/}"
                        i=$((i+1))
                    fi
                    cd ..
                fi
            done
            
            if [ $i -eq 1 ]; then
                echo "$(get_text "all_synced")"
                read -p "$(get_text "press_enter")"
                continue
            fi
            
            echo
            echo "$(get_text "batch_operations")"
            echo "1. $(get_text "stage_commit_all_repos")"
            echo "2. $(get_text "stage_commit_selected")"
            read -p "$(get_text "input_choice") [1-2]: " batch_choice
            
            case $batch_choice in
                1)
                    echo "$(get_text "enter_unified_msg")"
                    read -p "$(get_text "message"): " batch_msg
                    if [ -n "$batch_msg" ]; then
                        for dir in */; do
                            if [ -d "$dir/.git" ]; then
                                cd "$dir"
                                if ! git status | grep -q "nothing to commit"; then
                                    echo "$(get_text "processing") ${dir%/}"
                                    git add .
                                    git commit -m "$batch_msg"
                                    echo "✅ $(get_text "changes_committed")"
                                fi
                                cd ..
                            fi
                        done
                        echo "$(get_text "batch_complete")"
                    else
                        echo "$(get_text "msg_empty")"
                    fi
                    ;;
                2)
                    echo "$(get_text "select_repos")"
                    read -p "$(get_text "repo_numbers"): " repo_nums
                    
                    echo "$(get_text "enter_commit_msg")"
                    read -p "$(get_text "message"): " commit_msg
                    
                    if [ -n "$repo_nums" ] && [ -n "$commit_msg" ]; then
                        for num in $repo_nums; do
                            repo="${changed_repos[$num]}"
                            if [ -n "$repo" ]; then
                                echo "$(get_text "processing"): $repo"
                                cd "$repo"
                                git add .
                                git commit -m "$commit_msg"
                                echo "✅ $(get_text "changes_committed")"
                                cd ..
                            fi
                        done
                        echo "$(get_text "selected_complete")"
                    else
                        echo "$(get_text "msg_empty")"
                    fi
                    ;;
                *)
                    echo "$(get_text "invalid_choice")"
                    ;;
            esac
            read -p "$(get_text "press_enter")"
            ;;
        5)
            change_base_directory
            ;;
        6)
            switch_language
            read -p "$(get_text "press_enter")"
            ;;
        7)
            echo
            if [ "$CURRENT_LANG" = "zh_CN" ]; then
                read -p "确认要退出吗？(y/N): " confirm
            else
                read -p "Are you sure you want to exit? (y/N): " confirm
            fi
            
            if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
                echo "$(get_text "goodbye")"
                exit 0
            fi
            ;;
        *)
            echo "$(get_text "invalid_choice")"
            read -p "$(get_text "press_enter")"
            ;;
    esac
done